package cn.liuyi.weiyi;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CookieStore;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import cn.edu.pku.tools.SharedPreferencesHelper;
import cn.edu.pku.tools.YJRHttpClient;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;

public class ChangePhoneActivity extends SherlockActivity implements OnClickListener{

	LayoutInflater inflater;
	ActionBar actionBar;
	EditText new_phone_et;
	TextView change_phone_tv;
	View title_bar;
	Context context;
	String response;
	JSONObject jsonInfo;
	String phone, new_phone;
	SharedPreferencesHelper spHelper;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_change_phone);
		inflater = this.getLayoutInflater();
		actionBar = getSupportActionBar();
		context = this;
		this.spHelper = new SharedPreferencesHelper(this.context);
		phone = spHelper.getStringValue("phone");
		initTitleBar();
		
		initComponent();
		
		initEventListener();
		
		
		
	
	}
	
	private void initTitleBar(){
		ActionBar.LayoutParams lp = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,ActionBar.LayoutParams.WRAP_CONTENT,Gravity.CENTER);
		title_bar = inflater.inflate(R.layout.change_phone_action_bar, null);
		
		actionBar.setCustomView(title_bar,lp);
		actionBar.setDisplayShowHomeEnabled(false);
		actionBar.setDisplayShowTitleEnabled(false);
		actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
		actionBar.setDisplayShowCustomEnabled(true);
	}
	private void initComponent(){
		new_phone_et = (EditText) findViewById(R.id.new_phone_et);
		change_phone_tv = (TextView) findViewById(R.id.change_phone_tv);
	}
	
	private void initEventListener(){
		change_phone_tv.setOnClickListener(this);

		
	}

	@SuppressLint("NewApi")
	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.change_phone_tv:
			new_phone = new_phone_et.getText().toString();
			if(new_phone==null||new_phone.equals("")){
				Toast.makeText(context, "新的手机号不能为空", Toast.LENGTH_SHORT).show();
			}else if(phone.equals(new_phone)){
				Toast.makeText(context, "请修改手机号后再提交", Toast.LENGTH_SHORT).show();
			}else{
				changePhone();
			}
			break;
		default:
				break;
		}
		
	}
	
	private void changePhone(){
		final String phone_ = new_phone;
		RequestParams params = new RequestParams(); 
		params.add("phone", new_phone);
		YJRHttpClient.post(context,"/user/changePhone", params, new JsonHttpResponseHandler(){
			 @Override  
	            public void onSuccess(JSONObject data) {  
	             	jsonInfo = data;
	             	try {
						if(jsonInfo.getInt("result") == 1){
							YJRHttpClient.setCookie(context); 
							Toast.makeText(context, "新手机号可用，请填入手机验证码", Toast.LENGTH_LONG).show();
							Intent intent = new Intent(ChangePhoneActivity.this, CheckCaptchaActivity.class);
							intent.putExtra("phone", new_phone);
							startActivity(intent);
						}else{
							Toast.makeText(context, jsonInfo.getString("info"), Toast.LENGTH_SHORT).show();
						}
					} catch (JSONException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
	            }  
		});
	}
	

}